package com.zyh.em.util;

import com.zyh.em.entity.EvaluateData;
import com.zyh.em.entity.EvaluateReport;
import com.zyh.em.entity.GeoPoint;
import com.zyh.em.entity.LoginSuccessData;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ParseLogUtil {
    static final String regex = "INFO\\s+(\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2})\\s+([a-z0-9\\u4e00-\\u9fa5]*)\\s+(EVALUATE)\\s+\\[([a-z0-9\\u4e00-\\u9fa5]*)\\]\\s+([a-z0-9]{32})\\s+\\\"([a-z0-9\\.\\-\\,]{6,12})\\\"\\s+([a-z\\u4e00-\\u9fa5]*)\\s+\\\"([0-9\\.\\,]*)\\\"\\s+\\[([0-9\\,\\.]*)\\]\\s+\\\"(.*)\\\"";
    static final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
    //////////////////////////////////////////////////////////////////////////////////////
    public static boolean isEvaluateLog(String log){
        Matcher matcher = pattern.matcher(log);
        if (matcher.find()){
            if (matcher.group(3).equals("EVALUATE")){
                return true;
            }
        }
        return false;
    }
    public static boolean isLoginSuccessLog(String log){
        Matcher matcher = pattern.matcher(log);
        if (matcher.find()){
            if (matcher.group(3).equals("SUCCESS")){
                return true;
            }
        }
        return false;
    }
    public static EvaluateReport evaluateData2EvaluateReport(EvaluateData evaluateData){
        EvaluateReport evaluateReport = new EvaluateReport();
        evaluateReport.setAppName(evaluateData.getAppName());
        evaluateReport.setCity(evaluateData.getCity());
        evaluateReport.setGeoPoint(evaluateData.getGeoPoint());
        evaluateReport.setTime(evaluateData.getTime());
        evaluateReport.setUsername(evaluateData.getUsername());
        evaluateReport.setUuid(evaluateData.getUuid());
        return evaluateReport;
    }
    public static String parseLog2AppNameAndUsername(String log){
        Matcher matcher = pattern.matcher(log);
        if (matcher.find()){
            String appName = matcher.group(2);
            String username = matcher.group(4);
            return appName+":"+username;
        }
        return "";
    }
    //把登录成功的日志解析为登录成功对象 
    public static LoginSuccessData parseLog2LoginSuccessData(String log) {

        //正则表达式
        final String regex = "INFO\\s+(\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2})\\s+([a-z0-9\\u4e00-\\u9fa5]*)\\s+(SUCCESS)\\s+\\[([a-z0-9\\u4e00-\\u9fa5]*)\\]\\s+([a-z0-9]{32})\\s+\\\"([a-z0-9\\.\\-\\,]{6,12})\\\"\\s+([a-z\\u4e00-\\u9fa5]*)\\s+\\\"([0-9\\.\\,]*)\\\"\\s+\\[([0-9\\,\\.]*)\\]\\s+\\\"(.*)\\\"";
        //通过正则表达式以及匹配模式，生成的pattern对象
        final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);

        //pattern对需要处理的字符串进行匹配，生成的matcher
        final Matcher matcher = pattern.matcher(log);

//        System.out.println("matcher.find() = " + matcher.find());
        if (matcher.find()) {

            //处理时间的
            String group1 = matcher.group(1);
//            System.out.println("group1 = " + group1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long time = 0;//matcher.group(1)
            try {
                Date date = sdf.parse(group1);
                time = date.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }


            //处理地理位置
            String group8 = matcher.group(8);
            String[] split = group8.split(",");
            GeoPoint geoPoint = new GeoPoint(Double.parseDouble(split[0]), Double.parseDouble(split[1]));//matcher.group(8)

            //处理输入特征
            String group9 = matcher.group(9);
            String[] split1 = group9.split(",");
            double[] inputFeatures = new double[split1.length];//
            for (int i = 0; i < split1.length; i++) {
                inputFeatures[i] = Double.parseDouble(split1[i]);
            }

            LoginSuccessData loginSuccessData = new LoginSuccessData(time, matcher.group(2), matcher.group(4), matcher.group(5), matcher.group(6), matcher.group(7), geoPoint, inputFeatures, matcher.group(10));
            return loginSuccessData;
        }
        return null;
    }

       //把评估日志解析为评估对象
    public static EvaluateData parseLog2EvaluateData(String log){
        //正则表达式
        final String regex = "INFO\\s+(\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2})\\s+([a-z0-9\\u4e00-\\u9fa5]*)\\s+(EVALUATE)\\s+\\[([a-z0-9\\u4e00-\\u9fa5]*)\\]\\s+([a-z0-9]{32})\\s+\\\"([a-z0-9\\.\\-\\,]{6,12})\\\"\\s+([a-z\\u4e00-\\u9fa5]*)\\s+\\\"([0-9\\.\\,]*)\\\"\\s+\\[([0-9\\,\\.]*)\\]\\s+\\\"(.*)\\\"";

        //通过正则表达式以及匹配模式，生成的pattern对象
        final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);

        //pattern对需要处理的字符串进行匹配，生成的matcher
        final Matcher matcher = pattern.matcher(log);
        if(matcher.find()){
            //处理时间的
            String group1 = matcher.group(1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long time=0;//matcher.group(1)
            try {
                Date date = sdf.parse(group1);
                time=date.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }


            //处理地理位置
            String group8 = matcher.group(8);
            String[] split = group8.split(",");
            GeoPoint geoPoint=new GeoPoint(Double.parseDouble(split[0]),Double.parseDouble(split[1]));//matcher.group(8)

            //处理输入特征
            String group9 = matcher.group(9);
            String[] split1 = group9.split(",");
            double[] inputFeatures=new double[split1.length];//
            for (int i = 0; i < split1.length; i++) {
                inputFeatures[i]=Double.parseDouble(split1[i]);
            }

            EvaluateData evaluateData=new EvaluateData(time,matcher.group(2),matcher.group(4),matcher.group(5),matcher.group(6),matcher.group(7),geoPoint,inputFeatures,matcher.group(10));
            return evaluateData;
        }

        return null;
    }
}